package com.six666.medicaldevicetestingsystem.controller;


import com.six666.medicaldevicetestingsystem.dto.Personal_Compare_DTO;
import com.six666.medicaldevicetestingsystem.dto.UserAccount_time_DTO;
import com.six666.medicaldevicetestingsystem.entity.*;
import com.six666.medicaldevicetestingsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.lang.Math.random;
@CrossOrigin
@RestController
public class Method_Controller {
    @Autowired
    private ProjectFile_Service projectfile_service;
    @Autowired
    private ProjectTest_Service projecttest_service;
    @Autowired
    private TestOffiers_Service testoffiers_service;
    @Autowired
    private Project_Service project_service;
    @Autowired
    private Account_Service account_service;
    @Autowired
    private File_Service file_service;

    @PostMapping("/upload_operation_procedure")
    public BaseResponse<Boolean> upload_operation_procedure(@RequestParam("file") MultipartFile file,
                                                            @RequestParam("project_id") int project_id) {
        if (file.isEmpty()) {
            return new BaseResponse<>(101,null,"参数错误","文件为空");
        }
        String fileName = random()+"_operation_procedure" + project_id + "_#" + file.getOriginalFilename();
        boolean res=file_service.save_file(file,"pdf",fileName);
        if (!res){
            return new BaseResponse<>(false);
        }
        projectfile_service.add_project_file(project_id,"Regulations","pdf/"+fileName,fileName);
        return new BaseResponse<>(res);
    }

    @PostMapping("/download_operation_procedure")
    public BaseResponse<List<String>> get_procedure_address(@RequestParam Map<String, String> params) {
        int project_id;
        if (params.get("project_id")!=null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        else{
            return new BaseResponse<>(101,null,"参数错误","必填值为空");
        }
        try{
            List<String> data=projectfile_service.get_op_url_by_project_id__filetype(project_id,"Regulations");
            return new BaseResponse<>(data);
        }
        catch (Exception e){
            return new BaseResponse<>(102, null, "参数错误", "没找到");
        }
    }
    @PostMapping("/upload_compare_plan")
    public BaseResponse<Boolean> upload_compare_plan(@RequestParam("file") MultipartFile file,
                                                            @RequestParam("project_id") int project_id) {
        if (file.isEmpty()) {
            return new BaseResponse<>(101,null,"参数错误","文件为空");
        }
        String fileName = random()+"_compare_plan" + project_id + "_#" + file.getOriginalFilename();
        boolean res=file_service.save_file(file,"pdf",fileName);
        if (!res){
            return new BaseResponse<>(false);
        }
        projectfile_service.delete_file_by_project_id_and_type(project_id,"ComparePlan");
        projectfile_service.add_project_file(project_id,"ComparePlan","pdf/"+fileName,fileName);
        return new BaseResponse<>(res);
    }
    @PostMapping("/download_compare_plan")
    public BaseResponse<String> get_compare_plan(@RequestParam Map<String, String> params) {
        int project_id;
        if (params.get("project_id")!=null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        else{
            return new BaseResponse<>(101,null,"参数错误","必填值为空");
        }
       try{
           String data=projectfile_service.get_cp_url_by_project_id__filetype(project_id,"ComparePlan");
           return new BaseResponse<>(data);
       }
       catch (Exception e){
           return new BaseResponse<>(102, null, "参数错误", "没找到");
       }

    }

    @PostMapping("/create_compare_plan")
    public BaseResponse<Boolean> create_compare_plan(@RequestParam Map<String, String> params) {
        Date date = new Date(System.currentTimeMillis());
        ProjectTest projectTest = new ProjectTest();
        projectTest.setProjectid(Integer.parseInt(params.get("project_id")));
        projectTest.setState("Ongoing");
        projectTest.setEpoch(1);
        projectTest.setTesttime(date);
        int accountid=Integer.parseInt(params.get("account_id"));
        try{
            boolean data=projecttest_service.create_compare_plan(projectTest,accountid);
            return new BaseResponse<>(data);
        }
        catch (Exception e){
            e.printStackTrace();
            return new BaseResponse<>(101,null,"参数错误","");
        }
    }

    @PostMapping("/show_my_compare_plan")
    public BaseResponse<List<Personal_Compare_DTO>> show_my_compare_plan(@RequestParam Map<String, String> params) {
        int account_id=Integer.parseInt(params.get("account_id"));
        List<Integer> test_id_list=testoffiers_service.get_my_compare_plan_id(account_id);
        List<ProjectTest> test=projecttest_service.get_my_test(test_id_list);
        List<Personal_Compare_DTO> data=project_service.get_my_project(test);
        return new BaseResponse<>(data);
    }

    @PostMapping("/show_project_test_ongoing")
    public BaseResponse<UserAccount_time_DTO> show_all_test(@RequestParam Map<String, String> params) {
        UserAccount_time_DTO data=new UserAccount_time_DTO();
        int project_id=Integer.parseInt(params.get("project_id"));
        Pair<List<Integer>,Date> ongoing_test_id_list_time=projecttest_service.get_project_test_id(project_id);
        data.setDate(ongoing_test_id_list_time.second);
        List<Integer> ongoing_test_account_id_list=testoffiers_service.get_account_id_by_test_id(ongoing_test_id_list_time.first);
        data.setUserAccountList(account_service.get_by_id_list(ongoing_test_account_id_list));
        return new BaseResponse<>(data);
    }
    @GetMapping("/show_all_test")
    public BaseResponse<List<Personal_Compare_DTO>> get_all_test() {
        List<Integer> test_id_list=testoffiers_service.get_my_compare_plan_id(-1);
        List<ProjectTest> test=projecttest_service.get_my_test(test_id_list);
        List<Personal_Compare_DTO> data=project_service.get_my_project(test);
        return new BaseResponse<>(data);
    }
    @PostMapping("/finish_test_by_project_id")
    public BaseResponse<Boolean> finish_test_by_project_id(@RequestParam Map<String, String> params) {
        int project_id=Integer.parseInt(params.get("project_id"));
        try{
            boolean data=projecttest_service.finish_by_project_id(project_id);
            return new BaseResponse<>(data);
        }
        catch (Exception e){
            return new BaseResponse<>(101,null,"参数错误","项目id不存在");
        }
    }
    @PostMapping("/finish_test_by_test_id")
    public BaseResponse<Boolean> finish_test(@RequestParam Map<String, String> params) {
        int test_id=Integer.parseInt(params.get("test_id"));
        try{
            boolean data=projecttest_service.finish_by_test_id(test_id);
            return new BaseResponse<>(data);
        }
        catch (Exception e){
            return new BaseResponse<>(101,null,"参数错误","项目id不存在");
        }
    }
    @PostMapping("/delete_operation_procedure_and_compare_plan")
    public BaseResponse<Boolean> delete_operation_procedure(@RequestParam Map<String, String> params) {
        String file_address=params.get("file_address");
        try{
            boolean data=projectfile_service.delete_file_by_address(file_address);
            return new BaseResponse<>(data);
        }
        catch(Exception e){
            return new BaseResponse<>(101,null,"参数错误","文件地址不存在");
        }

    }
}
